package org.zhucy.queue;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * FIFO
 */
public class LinkedBlockingQueueDemo {

    public static void main(String[] args) {
        LinkedBlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<>(5);
        /*new Thread(()->{
            for(int i=0;i<Integer.MAX_VALUE;i++){
                try {
                    System.out.println("add............");
                    blockingQueue.put(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });*/
        new Thread(()->{
            try {
                while(true) {
                    Thread.sleep(2000L);
                    Integer num = blockingQueue.take();
                    System.out.printf("get queue value:%d\n", num);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }).start();
        new Thread(()->{
                while(true){
                    try {
                        Thread.sleep(5000L);
                        System.out.printf("queue size:%d\n",blockingQueue.size());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
        }).start();
        for(int i=0;i<Integer.MAX_VALUE;i++){
            try {
                System.out.println("add............");
                blockingQueue.put(i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
